package com.mmp.main.servlet.joborder;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.entity.JobOrderSparePart;
import com.mmp.main.util.WebUtil;

/**
 * Servlet implementation class JobOrderFollowupThirdPhaseProcessServlet
 */
@MultipartConfig
public class JobOrderFollowupThirdPhaseProcessServlet extends JobOrderTrackingProcessServlet {
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		if (!skipActionProcessing(request)) {

			int jobOrderId = WebUtil.getParamValueAsInteger(request, "jobOrderId", 0);

			JobOrder order = dbQueryManager.findById(jobOrderId, JobOrder.class);

			String[] joRequestFormUrl = FileUtil.uploadFiles(request, "joRequestFormUrl");
			// if (joRequestFormUrl != null && joRequestFormUrl.length != 0)
			order.setRequestFormUrl(Common.concatenateValues(joRequestFormUrl));

			if (order.getSparePartsOutsideQuotation() == null)
				order.setSparePartsOutsideQuotation(new ArrayList<JobOrderSparePart>());
			else
				order.getSparePartsOutsideQuotation().clear();

			setSparePartsOutsideQuotation(request, order);
			setFinalActionDetails(order, request);

			dbQueryManager.mergeEntity(order);

			if (request.getParameter("showJobOrderReport") == null)
				setMessage(request, new Message("تم الحفظ بنجاح", Message.MESSAGE_TYPE.SUCCESS));
		}

		updateNotificationsThenGoForward(request, response);

	}

	private void setSparePartsOutsideQuotation(HttpServletRequest request, JobOrder order)
			throws FileNotFoundException, IOException, ServletException {

		String jobOrderSpareParts_rowOrder = request.getParameter("extraSpareParts_rowOrder");
		if (jobOrderSpareParts_rowOrder != null && !jobOrderSpareParts_rowOrder.isEmpty()) {
			String[] indexes = jobOrderSpareParts_rowOrder.split(",");
			int quantity = 0;
			float price = 0;
			for (int i = 0; i < indexes.length; i++) {
				JobOrderSparePart sPart = new JobOrderSparePart();
				quantity = WebUtil.getParamValueAsInteger(request, "extraSpareParts_accQuantity_" + indexes[i], 0);
				price = 0;// WebUtil.getParamValueAsFloat(request,
							// "extraSpareParts_accPrice_" + indexes[i], 0f);

				sPart.setInQuotation(false);
				sPart.setQuantity(quantity);
				sPart.setPrice(price);
				sPart.setTotalPrice(quantity * price);
				sPart.setDescription(WebUtil.getParamValue(request, "extraSpareParts_accDescription_" + indexes[i],
						null));
				order.addSparePartsOutsideQuotation(sPart);
			}
		}
	}

}
